package com.snail.snailvr.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.snail.snailvr.R;
import com.snail.snailvr.VRApp;

/**
 * 页面加载动画
 * Created by snail on 16/6/27.
 */
public class LoadingView extends View {

    private static final int SCALE_UP=0,SCALE_DOWN=1;
    private static final long DELAY_TIME = 30;
    private int mScaleType=SCALE_DOWN;
    private Paint mPaint;
    private Paint mTextPaint;
    private float mRadius = 60f;
    private float mCurrentRadius;
    private float mDegrees = 0;
    private RectF rectF ;
    private DrawHandler mHandler;
    private int mStrokeWidth;
    private String mLoading;
    private int mCircleSize;
    private int mPadding;
    private int mScreenWidth,mScreenHeight;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        mDegrees-=10;
        scaleRadius();

        canvas.save();
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(mScreenWidth / 2, mScreenHeight / 2, mCurrentRadius, mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);

        canvas.drawArc(rectF, mDegrees,80
                , false, mPaint);

        canvas.drawArc(rectF, mDegrees+180,80
                , false, mPaint);

        canvas.drawText(mLoading,(mScreenWidth-mCircleSize) / 2,mScreenHeight/2+mCircleSize+mPadding,mTextPaint);

        canvas.restore();

    }

    private void scaleRadius() {
        if(mScaleType==SCALE_DOWN){
            mCurrentRadius-=1;
            if(mCurrentRadius<=0){
                mCurrentRadius=0;
                mScaleType=SCALE_UP;
            }
        }else{
            mCurrentRadius+=1;
            if(mCurrentRadius>=mRadius){
                mCurrentRadius=mRadius;
                mScaleType=SCALE_DOWN;
            }
        }
    }


    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(getResources().getColor(R.color.progress_color));

        mTextPaint= new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(getResources().getColor(R.color.download_item_total_size_color));
        mTextPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.text_size_11));

        DisplayMetrics displayMetrics= VRApp.getInstance().getResources().getDisplayMetrics();
        mScreenWidth=displayMetrics.widthPixels;
        mScreenHeight=displayMetrics.heightPixels*4/5;

        mHandler=new DrawHandler();
        mHandler.sendEmptyMessage(0);

        mCircleSize=getResources().getDimensionPixelSize(R.dimen.loading_circle_size);
        rectF = new RectF((mScreenWidth-mCircleSize)/2,(mScreenHeight-mCircleSize)/2,(mScreenWidth+mCircleSize)/2,(mScreenHeight+mCircleSize)/2);
        mLoading=getResources().getString(R.string.loading);
        mStrokeWidth=getResources().getDimensionPixelSize(R.dimen.loading_view_stroke_width);
        mRadius=getResources().getDimensionPixelSize(R.dimen.loading_view_radio_width);
        mPadding=getResources().getDimensionPixelSize(R.dimen.padding_12);

    }
    class DrawHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            invalidate();
            mHandler.sendEmptyMessageDelayed(0,DELAY_TIME);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        removeMessage();

    }

    private void removeMessage() {
        if(mHandler!=null)
            mHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if(visibility!=VISIBLE)
            removeMessage();
    }
}